﻿'© 2004 - 2010 ActiveModules, Inc. All Rights Reserved
Imports Microsoft.ApplicationBlocks.Data
Namespace DotNetNuke.Modules.ActiveForums.Data
    Public Class Common
        Inherits DataConfig
#Region "Security"

        Public Sub SavePermissionSet(ByVal PermissionSetId As Integer, ByVal PermissionSet As String)
            SqlHelper.ExecuteNonQuery(_connectionString, dbPrefix & "Permissions_Save", PermissionSetId, PermissionSet)
        End Sub
        Public Function GetRoles(ByVal SiteId As Integer) As IDataReader
            Return SqlHelper.ExecuteReader(_connectionString, dbPrefix & "Permissions_GetRoles", SiteId)
        End Function
        Public Function GetPermSet(ByVal PermissionsId As Integer, ByVal Key As String) As String
            Dim sSQL As String = "SELECT IsNULL(Can" & Key & ",'||||') from " & dbPrefix & "Permissions WHERE PermissionsId = " & PermissionsId
            Return CType(SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, sSQL), String)
        End Function
        Public Function SavePermSet(ByVal PermissionsId As Integer, ByVal Key As String, ByVal PermSet As String) As String
            Dim sSQL As String = "UPDATE " & dbPrefix & "Permissions SET Can" & Key & " = '" & PermSet & "' WHERE PermissionsId = " & PermissionsId
            SqlHelper.ExecuteNonQuery(_connectionString, CommandType.Text, sSQL)
            Return GetPermSet(PermissionsId, Key)
        End Function
        Public Function CreatePermSet(ByVal AdminRoleId As String) As Integer
            Return CType(SqlHelper.ExecuteScalar(_connectionString, dbPrefix & "Permission_Create", AdminRoleId), Integer)
        End Function
        ' KR - added caching
        Public Function CheckForumIdsForView(ByVal ForumIds As String, ByVal UserRoles As String) As String
            Dim cacheKey As String = String.Format("AF-Perm-{0}", ForumIds)
            Dim sSQL As String = "SELECT f.ForumId, ISNULL(CanView,'') as CanView from " & dbPrefix & "Permissions as P INNER JOIN " & dbPrefix & "Forums as f on f.PermissionsID = P.PermissionsId  INNER JOIN " & dbPrefix & "Functions_Split('" & ForumIds & "',':') as ids on ids.id = f.ForumId"
            Dim sForums As String = String.Empty
            Dim dt As DataTable
            Dim row As DataRow

            Dim data As Object = DataCache.CacheRetrieve(cacheKey)

            If (Not data Is Nothing) Then
                dt = CType(data, DataTable)
            Else
                dt = DotNetNuke.Common.ConvertDataReaderToDataTable(SqlHelper.ExecuteReader(_connectionString, CommandType.Text, sSQL))
                DataCache.CacheStore(cacheKey, dt)
            End If

            For Each row In dt.Rows
                Dim canView As String = row("CanView").ToString
                If Permissions.HasPerm(canView, UserRoles) Then
                    sForums &= row("ForumId").ToString & ":"
                End If
            Next

            Return sForums
        End Function
        Public Function SecurityUpgraded() As Boolean
            Dim sSQL As String = "SELECT Count(PermissionsId) FROM " & dbPrefix & "Permissions "
            Dim secCount As Integer = CType(SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, sSQL), Integer)
            If secCount > 0 Then
                Return True
            Else
                Return False
            End If

        End Function

#End Region
#Region "Views"
        Public Function UI_ActiveView(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal RowIndex As Integer, ByVal MaxRows As Integer, ByVal Sort As String, ByVal IsSuper As Boolean, ByVal TimeFrame As Integer, ByVal ForumIds As String) As DataSet
            Return CType(SqlHelper.ExecuteDataset(_connectionString, dbPrefix & "UI_ActiveView", PortalId, ModuleId, UserId, RowIndex, MaxRows, Sort, IsSuper, TimeFrame, ForumIds), DataSet)
        End Function
        Public Function UI_NotReadView(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal RowIndex As Integer, ByVal MaxRows As Integer, ByVal Sort As String, ByVal ForumIds As String) As DataSet
            Return CType(SqlHelper.ExecuteDataset(_connectionString, dbPrefix & "UI_NotRead", PortalId, ModuleId, UserId, RowIndex, MaxRows, Sort, ForumIds), DataSet)
        End Function
        Public Function UI_UnansweredView(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal RowIndex As Integer, ByVal MaxRows As Integer, ByVal Sort As String, ByVal ForumIds As String) As DataSet
            Return CType(SqlHelper.ExecuteDataset(_connectionString, dbPrefix & "UI_UnansweredView", PortalId, ModuleId, UserId, RowIndex, MaxRows, Sort, ForumIds), DataSet)
        End Function
        Public Function UI_TagsView(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal RowIndex As Integer, ByVal MaxRows As Integer, ByVal Sort As String, ByVal ForumIds As String, ByVal TagId As Integer) As DataSet
            Return CType(SqlHelper.ExecuteDataset(_connectionString, dbPrefix & "UI_TagsView", PortalId, ModuleId, UserId, RowIndex, MaxRows, Sort, ForumIds, TagId), DataSet)
        End Function
#End Region
#Region "TagCloud"
        Public Function TagCloud_Get(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumIds As String, ByVal Rows As Integer) As IDataReader
            Return SqlHelper.ExecuteReader(_connectionString, dbPrefix & "UI_TagCloud", PortalId, ModuleId, ForumIds, Rows)
        End Function
#End Region
#Region "Tags"
        Public Function Tag_GetIdByName(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal TagName As String, ByVal IsCategory As Boolean) As Integer
            Return CType(SqlHelper.ExecuteScalar(_connectionString, dbPrefix & "Tags_GetByName", PortalId, ModuleId, TagName.Replace("-", " ").ToLowerInvariant, IsCategory), Integer)
        End Function
#End Region
#Region "TopMembers"
        Public Function TopMembers_Get(ByVal PortalId As Integer, ByVal Rows As Integer) As IDataReader
            Return SqlHelper.ExecuteReader(_connectionString, dbPrefix & "UI_TopMembers", PortalId, Rows)
        End Function
#End Region
#Region "CustomURLS"
        Public Function GetPrefixes(ByVal PortalId As Integer) As Dictionary(Of String, String)
            Dim dict As New Dictionary(Of String, String)
            Using dr As IDataReader = SqlHelper.ExecuteReader(_connectionString, dbPrefix & "Forums_GetPrefixes", PortalId)
                While dr.Read()
                    Dim prefix As String = dr("PrefixURL").ToString
                    Dim tabid As String = dr("TabId").ToString
                    Dim forumid As String = dr("ForumId").ToString
                    Dim moduleId As String = dr("ModuleId").ToString
                    Dim archived As String = dr("Archived").ToString
                    Dim forumgroupId As String = dr("ForumGroupId").ToString
                    Dim groupPrefix As String = dr("GroupPrefixURL").ToString
                    If Not String.IsNullOrEmpty(groupPrefix) Then
                        prefix = groupPrefix & "/" & prefix
                    End If
                    dict.Add(prefix, tabid & "|" & forumid & "|" & moduleId & "|" & archived & "|" & forumgroupId & "|" & groupPrefix)
                End While
                dr.Close()
            End Using
            Return dict
        End Function
        Public Function GetUrl(ByVal ModuleId As Integer, ByVal ForumGroupId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal UserId As Integer, ByVal ContentId As Integer) As String
            Try
                Return CType(SqlHelper.ExecuteScalar(_connectionString, dbPrefix & "Util_GetUrl", ModuleId, ForumGroupId, ForumId, TopicId, UserId, ContentId), String)
            Catch ex As Exception
                Return String.Empty
            End Try
        End Function
        Public Function FindByURL(ByVal PortalId As Integer, ByVal URL As String) As IDataReader
            Return SqlHelper.ExecuteReader(_connectionString, dbPrefix & "FindByURL", PortalId, URL)
        End Function
        Public Function URLSearch(ByVal PortalId As Integer, ByVal URL As String) As IDataReader
            Return SqlHelper.ExecuteReader(_connectionString, dbPrefix & "URL_Search", PortalId, URL)
        End Function
        Public Sub ArchiveURL(ByVal PortalId As Integer, ByVal ForumGroupId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal URL As String)
            SqlHelper.ExecuteNonQuery(_connectionString, dbPrefix & "URL_Archive", PortalId, ForumGroupId, ForumId, TopicId, URL)
        End Sub
        Public Function CheckForumURL(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal VanityName As String, ByVal ForumId As Integer, ByVal ForumGroupId As Integer) As Boolean
            Try
                Dim _mainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
                Dim fgc As New ForumGroupController
                Dim fg As ForumGroupInfo = fgc.GetForumGroup(ModuleId, ForumGroupId)
                If Not String.IsNullOrEmpty(fg.PrefixURL) Then
                    VanityName = fg.PrefixURL & "/" & VanityName
                End If
                If Not String.IsNullOrEmpty(_mainSettings.PrefixURLBase) Then
                    VanityName = _mainSettings.PrefixURLBase & "/" & VanityName
                End If
                Dim tmpForumId As Integer = -1
                tmpForumId = CType(SqlHelper.ExecuteScalar(_connectionString, dbPrefix & "URL_CheckForumVanity", PortalId, VanityName), Integer)
                If tmpForumId > 0 And ForumId = -1 Then
                    Return False
                ElseIf tmpForumId = ForumId And ForumId > 0 Then
                    Return True
                ElseIf tmpForumId <= 0 Then
                    Return True
                End If
            Catch ex As Exception
                Return False

            End Try
        End Function
        Public Function CheckGroupURL(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal VanityName As String, ByVal ForumGroupId As Integer) As Boolean
            Try
                Dim _mainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
                If Not String.IsNullOrEmpty(_mainSettings.PrefixURLBase) Then
                    VanityName = _mainSettings.PrefixURLBase & "/" & VanityName
                End If
                Dim tmpForumGroupId As Integer = -1
                tmpForumGroupId = CType(SqlHelper.ExecuteScalar(_connectionString, dbPrefix & "URL_CheckGroupVanity", PortalId, VanityName), Integer)
                If tmpForumGroupId > 0 And ForumGroupId = -1 Then
                    Return False
                ElseIf tmpForumGroupId = ForumGroupId And ForumGroupId > 0 Then
                    Return True
                ElseIf tmpForumGroupId <= 0 Then
                    Return True
                End If
            Catch ex As Exception
                Return False

            End Try
        End Function

#End Region
    End Class
End Namespace
